<?php ob_start(); ?> 
<?php header ('Content-type: text/xml; charset=utf-8'); ?>     
<?php include "constants.inc" ?>
<?php include "classes.inc" ?>
<?php session_start(); ?>
<?php

function loadPlayer() {
    
    if ( !isset($_GET['rank']) || $_GET['rank'] == '0' || $_GET['rank'] == '' ) {
        
        $player = new Player;
        $player->player_key = '0';
        $player->player_name = 'FORFAIT';
        $player->elo = 0;
        return $player;
                
    } else if ( isset($_GET['rank']) && isset($_GET['row']) ) {
   		
        $playerRank = $_GET['rank'];
   		$playerBoard = $_GET['row'];
  		if (isset($_SESSION['homeTeam'])) {
        	$homeTeam = $_SESSION['homeTeam'];
      	}
      	mysql_connect(DB_SERVER, DB_USER, DB_PWD);
    	mysql_select_db(DB_CATALOG);
    	$result = mysql_query("select pl.player_list_id 
	    	from zt_player_list pl
	        join club c on c.club_id = pl.club_id 
	        join zt_team t on t.club_id = c.club_id
	        join zt_tournament tr on tr.tournament_id = pl.tournament_id
	        where tr.iscurrent = 1 and pl.date_approved is not null 
	        and t.team_id = ".$homeTeam." order by player_list_id desc");
    	$record = mysql_fetch_assoc($result);

	    $query = "select p.matricule, p.nom_prenom, p.elo_calcul, zp.team_id 
	    	from zt_player_list zpl, zt_player zp, playernew p 
	    	where zpl.player_list_id = zp.player_list_id 
	    	and zp.player_key = p.matricule and zpl.player_list_id = "
        	.$record['player_list_id']." order by zp.index_nr";
      	$result = mysql_query($query);
      	if (mysql_data_seek($result, $playerRank-1)) {
	        $row = mysql_fetch_assoc($result);
	        $player = new Player;
	  	    $player->player_key = $row['matricule'];
	  	    $player->player_name = $row['nom_prenom'];
	  	    $player->elo = $row['elo_calcul'];
	  	    $player->team_name = $row['team_id'];
	  	    
	  	    // validate if this player may play in this team
	  	    
	  	    if (!empty($player->team_name)) {
	  	    	// effective player
	  	    	$query = "select team_nr, group_id from zt_team where team_id = ".$player->team_name;
	  	    	$result = mysql_query($query);
	  	    	$row = mysql_fetch_assoc($result);
	  	    	$playerListTeam = $row['team_nr'];
	  	    	$playerListDepartment = $row['group_id'];
	  	    	
	  	    	$query = "select team_nr, group_id from zt_team where team_id = ".$homeTeam;
	  	    	$result = mysql_query($query);
	  	    	$row = mysql_fetch_assoc($result);
	  	    	$actualTeamPlayed = $row['team_nr'];
	  	    	$actualDepartment = $row['group_id'];
	  	    	
	  	    	if ($actualTeamPlayed > $playerListTeam) {
	  	    		// cannot play in lower team
	  	    		$player->error = "Bord ".$playerBoard.": enkel ploeg ".$playerListTeam." of hoger toegelaten";
	  	    	} else if ($playerListTeam != $actualTeamPlayed) {
	  	    		// played in higher team
	  	    		if ($actualDepartment == $playerListDepartment) {
	  	    			// but in same department, is not allowed
	  	    			$player->error = "Bord ".$playerBoard.": enkel ploeg ".$playerListTeam." toegelaten in deze afdeling";
	  	    		}
	  	    	}
	  	    }
	  	    
	  	    $previousBoard = $playerBoard-1;
	  	    if (isset($_SESSION['h'.$previousBoard])) {
	  	    	$previousRank = $_SESSION['h'.$previousBoard];
	  	    	if ($previousRank > $playerRank) {
	  	    		// player at wrong board
	  	    		$player->error = "Bord ".$playerBoard.": ongeldige opstelling";
	  	    	} else if ($previousRank == $playerRank) {
	  	    		// unlikely, but player mentioned twice
	  	    		$player->error = "Bord ".$playerBoard.": reeds opgesteld op bord ".$previousBoard;
	  	    	}
	  	    }
	  	    
	  	    $_SESSION['h'.$playerBoard] = $playerRank;
	  	    
	  	    return $player;
      	}
  }

  return null;
}

function getPlayerName() {
  	$player = loadPlayer();
  	return $player->player_name." (".$player->elo.")";
}

function getPlayerKey() {
  	$player = loadPlayer();
  	return $player->player_key;
}

function getPlayerError() {
  	$player = loadPlayer();
	if (isset($player->error) && !empty($player->error)) {
  		return $player->error;
  	}
  	return '0';
}

?>

<response>
  <method>getHomePlayerByRank</method>
  <key><?php echo getPlayerKey()?></key>
  <name><?php echo getPlayerName()?></name>
  <error><?php echo getPlayerError()?></error>
</response>